# TODO: Add comment
# 
# Author: yaping
###############################################################################


##scatterplot for HCT116 DKO1, GCH, HCG methylation changes
fudgeit <- function(){
	xm <- get('xm', envir = parent.frame(1))
	ym <- get('ym', envir = parent.frame(1))
	z  <- get('dens', envir = parent.frame(1))
	colramp <- get('colramp', parent.frame(1))
	image.plot(xm,ym,z, col = colramp(256), legend.only = T, add =F)
}

#color=heat.colors(26) 
#color=terrain.colors(26) 
#color=rainbow(100);
par(plt = c(0.1171429 ,0.8200000, 0.1457143, 0.8828571))
library(fields)
setwd("/Volumes/HD_2/Documents/lab/project/GENOMEseq/HCT116/summary_plot")
Lab.palette <- colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan","#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000"))

##methylation overview
pdf("HCG methylation heatmap in chr1.DKO1_vs_HCT116.pdf", paper="special", height=4, width=4)
par(mar = c(5,4,4,5) + .1)
plot(0,0,xlim=c(0,1),ylim=c(0,1),xlab="",ylab="")
rect(par("usr")[1], par("usr")[3], par("usr")[2], par("usr")[4], col ="#00007F")
par(new=T,mar = c(5,4,4,5) + .1)
smoothScatter(cbind(dko[,6],dko[,5]),nbin = 1000,bandwidth=0.001,colramp = Lab.palette,xlab="HCT116",ylab="DKO1",main="HCG methylation in chr1",xlim=c(0,1),ylim=c(0,1),nrpoints = 0, postPlotHook = fudgeit)
abline(0,1)
dev.off()

pdf("GCH methylation heatmap in chr1.DKO1_vs_HCT116.pdf", paper="special", height=4, width=4)
par(mar = c(5,4,4,5) + .1)
plot(0,0,xlim=c(0,1),ylim=c(0,1),xlab="",ylab="")
rect(par("usr")[1], par("usr")[3], par("usr")[2], par("usr")[4], col ="#00007F")
par(new=T)
smoothScatter(cbind(dko[,8],dko[,7]),nbin = 1000,bandwidth=0.001,colramp = Lab.palette,xlab="HCT116",ylab="DKO1",main="GCH methylation in chr1",xlim=c(0,1),ylim=c(0,1),nrpoints = 0, postPlotHook = fudgeit)
abline(0,1)
dev.off()

pdf("DKO1(chr1), GCH vs HCG.pdf", paper="special", height=4, width=4)
par(mar = c(5,4,4,5) + .1)
plot(0,0,xlim=c(0,1),ylim=c(0,1),xlab="",ylab="")
rect(par("usr")[1], par("usr")[3], par("usr")[2], par("usr")[4], col ="#00007F")
par(new=T)
smoothScatter(cbind(dko[,5],dko[,7]),nbin = 1000,bandwidth=0.001,colramp = Lab.palette,xlab="HCG_methy",ylab="GCH_methy",main="DKO1(chr1), GCH vs HCG",xlim=c(0,1),ylim=c(0,1),nrpoints = 0, postPlotHook = fudgeit)
abline(0,1)
dev.off()

pdf("HCT116(chr1), GCH vs HCG.pdf", paper="special", height=4, width=4)
par(mar = c(5,4,4,5) + .1)
plot(0,0,xlim=c(0,1),ylim=c(0,1),xlab="",ylab="")
rect(par("usr")[1], par("usr")[3], par("usr")[2], par("usr")[4], col ="#00007F")
par(new=T)
smoothScatter(cbind(dko[,6],dko[,8]),nbin = 1000,bandwidth=0.001,colramp = Lab.palette,xlab="HCG_methy",ylab="GCH_methy",main="HCT116(chr1), GCH vs HCG",xlim=c(0,1),ylim=c(0,1),nrpoints = 0, postPlotHook = fudgeit)
abline(0,1)
dev.off()


##methylation && accessible overview
pdf("Accessible_Diff vs Methylation_Diff (chr1).pdf", paper="special", height=4, width=4)
par(mar = c(5,4,4,5) + .1)
plot(0,0,xlim=c(-1,1),ylim=c(-1,1),xlab="",ylab="")
rect(par("usr")[1], par("usr")[3], par("usr")[2], par("usr")[4], col ="#00007F")
par(new=T)
smoothScatter(cbind(dko[,5]-dko[,6],dko[,7]-dko[,8]),nbin = 1000,bandwidth=0.001,colramp = Lab.palette,xlab="Methylation_Diff(DKO1-HCT116)",ylab="Accessible_Diff(DKO1-HCT116)",main="Accessible_Diff vs Methylation_Diff (chr1)",xlim=c(-1,1),ylim=c(-1,1),nrpoints = 0, postPlotHook = fudgeit)
abline(v=0)
abline(h=0)
abline(v=0.2,lty=2)
abline(h=0.2,lty=2)
abline(v=-0.2,lty=2)
abline(h=-0.2,lty=2)
dev.off()

